<?php
/**
 * Created by lni_wang.
 * Product: PhpStorm
 * Date: 2017/5/9 18:54
 * File: User_model.php
 */
class User_model extends MY_Model
{
    public function login(array $params)
    {
        data_filter($params);
        if(empty($params['username'])){
            throw new SysException(30003, ['username']);
        }
        if(empty($params['password'])){
            throw new SysException(30003, ['password']);
        }

        $user = $this->get_row('users', [
            'username' => $params['username']
        ]);

        if(empty($user)){
            throw new SysException(40002, ['username']);
        }

        if($user['password'] != $this->password($params['password'])){
            throw new SysException(40003, ['username']);
        }

        return [
            'user_id'  => $user['user_id'],
            'username' => $user['username'],
        ];
    }
    public function register(array $params)
    {
        data_filter($params);
        if(empty($params['username'])){
            throw new SysException(30003, ['username']);
        }
        if(empty($params['password'])){
            throw new SysException(30003, ['password']);
        }

        if($this->get_total('users', [
            'username' => $params['username'],
        ])){
            throw new SysException(40001);
        }

        $result = $this->insert('users', [
            'username' => $params['username'],
            'password' => $this->password($params['password']),
        ]);

        if($result){
            $result = $this->create_certificate([
                'user_id' => $this->insert_id('users')
            ]);
        }

        return $result;
    }

    private function password($password)
    {
        return md5('!@#345ERTfgh%^6%2(_5' . md5($password));
    }

    public function create_certificate(array $params)
    {
        if(empty($params['user_id'])){
            throw new SysException(30003, ['user_id']);
        }

        if($this->get_total('certificate', [
            'user_id' => $params['user_id'],
        ])){
            $result = $this->update('certificate', [
                'app_secret' => get_unique_key(),
            ],[
                'user_id' => $params['user_id'],
            ]);
        }else{
            $result = $this->insert('certificate', [
                'user_id'    => $params['user_id'],
                'app_id'     => $this->create_certificate_app_id(),
                'app_secret' => get_unique_key(),
            ]);
        }

        return $result;
    }

    private function create_certificate_app_id()
    {
        $app_id = get_unique_key();
        if($this->get_total('certificate', [
            'app_id' => $app_id,
        ])){
            return $this->create_certificate_app_id();
        }else{
            return $app_id;
        }

    }

    public function get_info(array $params)
    {
        data_filter($params);
        if(empty($params['user_id'])){
            throw new SysException(30003, ['user_id']);
        }

        $user = $this->get_row('users', [
            'user_id' => $params['user_id']
        ], 'user_id, username');

        if(empty($user)){
            throw new SysException(40002);
        }

        $certificate = $this->get_row('certificate', [
            'user_id' => $params['user_id']
        ], 'app_id, app_secret');


        return [
            'user'        => $user,
            'certificate' => $certificate,
        ];
    }

}